<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="销售订单编号" prop="soNumber">
        <el-input
          v-model="queryParams.soNumber"
          placeholder="请输入销售订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销售员" prop="salesPerson">
        <el-input
          v-model="queryParams.salesPerson"
          placeholder="请输入销售员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品编号" prop="productCode">
        <el-input
          v-model="queryParams.productCode"
          placeholder="请输入产品编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单日期" prop="orderDates">
        <el-date-picker
          clearable
          v-model="queryParams.orderDates"
          type="datetimerange"
          range-separator="至"
          value-format="yyyy-MM-dd"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-search" size="mini" @click="handleAllQuery">查询所有</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="planningList">
      <el-table-column type="index" label="序号" />
      <el-table-column label="销售员" align="center" prop="salesPerson" width="80" />
      <el-table-column label="销售订单号" align="center" prop="soNumber" width="200" />
      <el-table-column label="订单交期" align="center" prop="orderDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="生产计划号" align="center" prop="planCode" width="150" />
      <el-table-column label="产品编号" align="center" prop="itemCode" width="150" />
      <el-table-column label="产品名称" align="center" prop="itemName" width="150" />
      <el-table-column label="规格型号" align="center" prop="specification" />
      <el-table-column label="单位" align="center" prop="unitOfMeasure" />
      <el-table-column label="计划数量" align="center" prop="quantity" />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listPlanningDemand } from '@/api/mes/pro/planning';
import SourceSelect from '@/components/sourceSelect/single.vue';
import ItemSelect from '@/components/itemSelect/single.vue';
import ProductSelect from '@/components/productSelect/single.vue';
import PlanBom from '@/views/mes/pro/planning/bom/bom.vue';

export default {
  name: 'Planning',
  components: { PlanBom, ProductSelect, ItemSelect, SourceSelect },
  dicts: ['mes_workorder_sourcetype', 'pro_planning_status', 'mes_item_product'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产计划表格数据
      planningList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        soNumber: null,
        salesPerson: null,
        productCode: null,
        productName: null,
        beginTime: null,
        endTime: null
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询生产计划列表 */
    getList() {
      this.loading = true;
      this.queryParams.beginTime = null;
      this.queryParams.endTime = null;
      if (this.queryParams.orderDates) {
        this.queryParams.beginTime = this.queryParams.orderDates[0];
        this.queryParams.endTime = this.queryParams.orderDates[1];
      }
      listPlanningDemand(this.queryParams).then(response => {
        this.planningList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        planId: null,
        planCode: null,
        planName: null,
        orderSource: null,
        sourceCode: null,
        productId: null,
        productCode: null,
        productName: null,
        productSpc: null,
        unitOfMeasure: null,
        quantity: null,
        itemOrProduct: null,
        startDate: null,
        endDate: null,
        requestDate: null,
        status: 'PREPARE',
        remark: null
      };
      this.autoGenFlag = false;
      this.resetForm('form');
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
      this.getList();
    },
    /** 搜索按钮操作 */
    handleAllQuery() {
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 1000000;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm');
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        '/mes/pro/planning/download',
        {
          ...this.queryParams
        },
        `planning_${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>
